﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DotNetNuke.Entities.Tabs;
using System.Xml.Linq;
using System.Text.RegularExpressions;

using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Services.FileSystem;
using DotNetNuke.Services.Localization;


namespace company_present
{
    public partial class AddNews : ModuleCompanyBase
    {

        XElement elemts;

        private int GetValueFromConfigLand(string key)
        {

            int rtvl;

            if (elemts == null)
            {
                string savePath = System.IO.Path.Combine(Server.MapPath(ControlPath), "ConfigLand.xml");
                elemts = XElement.Load(savePath);
            }

            string vl = (from it in elemts.Elements("cat")
                         where it.Element("key").Value == key
                         select it).FirstOrDefault().Element("value").Value;

            rtvl = int.Parse(vl);

            return rtvl;

        }


        private string GenerateURL(string tabid, string newid, string title)
        {


            string temp = ReWriteUrl(newid, title);
            Regex rg = new Regex(@"/tabid/\d+/");
            temp = rg.Replace(temp, "/tabid/" + tabid + "/");

            return temp;

        }

        private string UrlImage(string name)
        {

            string rt = "";

            rt = "http://" + PortalAlias.HTTPAlias + "/Images/news/" + name;

            return rt;

        }

        private string UrlTab(int tabid_)
        {

            string rt = DotNetNuke.Common.Globals.NavigateURL(tabid_);

            return rt;

        }


        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void Button1_Click(object sender, EventArgs e)
        {

            var context = DB;

            //create dnn_d_news
            var news = new cp_New();

            if ((Title.Text != null) && ((DotNetNuke.UI.UserControls.TextEditor)txtDescription).Text != null && ((DotNetNuke.UI.UserControls.TextEditor)txtContent).Text != null)
            {

                string filename;
                if (ImageUpload.HasFile)
                {
                    filename = Guid.NewGuid() + ImageUpload.FileName;
                    
                   // ImageUpload.SaveAs(PathPhysicalImage + filename);

                    FileSystemUtils.UploadFile(PortalSettings.HomeDirectoryMapPath+"/images/", ImageUpload.PostedFile,filename);
                }
                else {
                    filename = "noimage.gif";
                }
                
                news.Img = filename;
                news.Title = Title.Text;
                news.Description = ((DotNetNuke.UI.UserControls.TextEditor)txtDescription).Text;
                news.Content = ((DotNetNuke.UI.UserControls.TextEditor)txtContent).Text;
                news.Visible = this.isVisible.Checked;
                news.CreateDate = DateTime.Now;
                news.TabID = TabId;

                context.cp_News.InsertOnSubmit(news);
                context.SubmitChanges();
           
            }
        }
    }
}